package com.xrui.hbase.codec.impl;

import com.google.common.base.Preconditions;
import com.xrui.hbase.DecodedCell;
import com.xrui.hbase.codec.CellDecoder;
import com.xrui.hbase.impl.BoundColumnReaderSpec;
import com.xrui.hbase.schema.SchemaType;
import com.xrui.hbase.schema.spec.CellSpec;
import com.xrui.hbase.schema.spec.ColumnReaderSpec;

import java.io.IOException;

/**
 * De-serializes an HBase cell as a raw byte array.
 *
 * @param <T> The type of the decoded cell data. Should be byte[].
 */
public final class RawBytesCellDecoder<T> implements CellDecoder<T> {
    public static final String PROVIDER_NAME = "RawBytesCellDecoder";

    /**
     * Initializes a new RawBytesCellDecoder.
     */
    public RawBytesCellDecoder() {
    }

    /**
     * Initializes a new RawBytesCellDecoder.
     *
     * @param cellSpec Specification of the cell encoding.
     */
    public RawBytesCellDecoder(CellSpec cellSpec) {
        Preconditions.checkNotNull(cellSpec);
        Preconditions.checkArgument(
            cellSpec.getCellSchema().getType() == SchemaType.RAW_BYTES);
    }

    /**
     * Initializes a new RawBytesCellDecoder.
     *
     * @param spec Specification of the cell encoding.
     */
    public RawBytesCellDecoder(BoundColumnReaderSpec spec) {
        Preconditions.checkNotNull(spec);
        Preconditions.checkArgument(
            spec.getColumnReaderSpec().getEncoding() == ColumnReaderSpec.Encoding.RAW_BYTES);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public DecodedCell<T> decodeCell(byte[] encodedBytes) throws IOException {
        return new DecodedCell(encodedBytes);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public T decodeValue(byte[] bytes) throws IOException {
        return (T) bytes;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getName() {
        return PROVIDER_NAME;
    }
}
